/**
 *
 */
package com.floreantpos.swing;

import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class DoubleDocument extends PlainDocument {

  /**
   * @param field
   */
  public DoubleDocument() {
  }

  @Override
  public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
    String value = getText(0, getLength());

    value = value + str;

    DecimalFormat decimalFormat = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(new Locale("pt", "BR")));

    try {
      decimalFormat.parse(value);
    } catch (Exception x) {
      Toolkit.getDefaultToolkit().beep();
      return;
    }

    super.insertString(offs, str, a);
  }
}
